package com.newsproject.mapper;
import com.newsproject.pojo.*;
import com.newsproject.pojo.DTO.AuditPostDTO;
import com.newsproject.pojo.DTO.HotSearchPostDTO;
import com.newsproject.pojo.DTO.UserDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AdminMapper {

/* 管理员对用户信息管理 */

//获取用户列表(分页查询)
      //start:偏移量,表示从第几条记录开始查询 pageSize:表示每页显示的数量
  List<UserDTO> getUserList(@Param("start") int start, @Param("pageSize") int pageSize);

//获取用户总数(用于分页)
    int getAllUserCount();

//根据关键词获取用户总数
    int getUsersCountByKeyword(@Param("keyword") String keyword);


//根据id获取用户信息
  User getUserById(@Param("userId") int userId);

//添加新用户
    void addUser(User user);
//初始化用户状态
    void addUserStatus(UserStatus userStatus);
//增加加密方式
    void updateUserPassword(@Param("userId") int userId,@Param("password") String password);

//更新用户信息
    void updateUser(User user);

//通过id删除用户
    void deleteUserById(@Param("userId") int userId);

//批量删除多个用户
    void deleteUserByIds(@Param("userIds") int[]userIds);

//模糊查询用户
    List<UserDTO> searchUsers(@Param("keyword") String keyword,@Param("start") int start,@Param("pageSize") int pageSize);

//根据用户id获取用户状态
@Select("select user_id from user_status where user_id = #{userId};")
   UserStatus getUserStatusById(@Param("userId") int userId);

//设置用户禁言状态
@Update("update user_status us join user u on u.user_id = us.user_id set us.is_banned = #{isBanned} where u.user_id = #{userId};")
void updateUserBanStatus(@Param("userId") int userId,@Param("isBanned") boolean isBanned);

//设置用户私信权限
@Update("update user_status us join user u on u.user_id = us.user_id set us.is_private_message = #{isPrivateMessage} where u.user_id = #{userId};")
void updatePrivateMessagePermission(@Param("userId") int userId,@Param("isPrivateMessage") boolean isPrivateMessage);

//设置用户账号状态
@Update("update user_status us join user u on u.user_id = us.user_id set us.ban_account = #{banAccount} where u.user_id = #{userId};")
void updateAccountStatus(@Param("userId") int userId,@Param("banAccount") boolean banAccount);

//批量设置用户权限
   void batchUpdateUserPermissions(@Param("list") List<UserStatus> updateList);

//根据用户名获取用户信息
    User getUserByName(String userName);

/* 管理员对热搜管理 */

//1.展示热搜列表
     //需要展示 (hot_search)hot_search_id keyword heat   (post)audit_status(审核状态) created_time title content
@Select("select h.keyword,h.hot_search_id as hotSearchId,p.title,p.content,t.tag_name as tagName,p.cover as postCover,u.username as authorName,p.created_time as createTime," +
"p.heat,h.is_blocked as isBlock,h.is_limited as isLimit from hot_search h join post p on h.hot_post_id = p.post_id join user u on p.user_id = u.user_id " +
"join tag t on p.tag_id = t.tag_id order by h.heat desc limit #{start},#{pageSize};")
   List<HotSearchPostDTO> getHotSearchList(@Param("start") int start, @Param("pageSize") int pageSize);
//2.获取热搜总数(用于分页)
@Select("select count(*) from hot_search;")
    int getHotSearchCount();
//根据关键词获取热搜总数
    int getHotSearchCountByKeyword(@Param("keyword") String keyword);

//3.根据id查询热搜信息
@Select("select h.hot_search_id,h.keyword,h.heat,p.cover as postCover,u.ver_content,u.username as authorName,t.tag_name ,p.created_time as createTime,p.title,p.content,h.is_limited as isLimit,h.is_blocked as isBlock,u.is_verified as isVerified,u.avatar " +
        "from hot_search h join post p on h.hot_post_id = p.post_id join user u on u.user_id = p.user_id join tag t on p.tag_id = t.tag_id where h.hot_search_id = #{HotSearchId};")
HotSearchPostDTO getHotSearchDTOById(@Param("HotSearchId") int HotSearchId);

//根据id获取热搜信息
@Select("select h.hot_search_id,h.keyword,h.heat,p.audit_status,p.created_time,p.title,p.content from hot_search h join post p on h.hot_post_id = p.post_id where h.hot_search_id = #{HotSearchId};")
HotSearch getHotSearchById(@Param("HotSearchId") int HotSearchId);

//根据id获取新闻信息
@Select("select p.post_id,p.user_id,u.username as userName,p.tag_id,t.tag_name as tagName,p.title,p.cover,p.content,p.audit_status,p.created_time,p.heat,p.is_member_only,p.picture from post p join user u on p.user_id = u.user_id join tag t on t.tag_id = p.tag_id where p.post_id = #{postId};")
   Post getPostById(@Param("postId") int postId);

/*
//4.添加热搜及初始化热搜状态
    void addHotSearch(Post post);
    void addHotSearchStatus(HotSearch hotSearch);
*/

//5.修改热搜部分信息(不再进行批量修改)
    void updateHotSearch(@Param("hd") HotSearchPostDTO dto);

//6.1 修改单个热搜状态(是否屏蔽)
@Update("update hot_search set is_blocked = #{isBlocked} where hot_search_id=#{hotSearchId};")
    void updateHotSearchBlocked(@Param("hotSearchId") int hotSearchId, @Param("isBlocked") boolean isBlocked);

//6.2 修改单个热搜状态(是否限流)
@Update("update hot_search set is_limited = #{isLimited} where hot_search_id=#{hotSearchId};")
     void updateHotSearchLimited(@Param("hotSearchId") int hotSearchId,@Param("isLimited") boolean isLimited);
// 更新热搜访问计数
@Update("update hot_search h join post p on h.hot_post_id=p.post_id set h.heat = p.view_count*0.5+p.comment_count*0.2+p.like_count*0.2+p.share_count*0.2 where hot_search_id=#{hot_search_id};")
    void incrementHotSearchViewCount(@Param("hotSearchId") int hotSearchId);
// 获取热搜限流状态
@Select("SELECT is_limited FROM hot_search WHERE hot_search_id = #{hotSearchId}")
    Boolean getHotSearchLimitStatus(@Param("hotSearchId") int hotSearchId);

//7.修改热搜(新闻)的状态(批量热搜)
    void updateHotSearchStatusBatch(@Param("list") List<HotSearch> updateList);

//8.根据id删除热搜
@Delete("delete from hot_search where hot_search_id=#{hot_search_id};")
    void deleteHotSearchById(@Param("hot_search_id") int hot_search_id);

//9.批量删除热搜
    void deleteHotSearchBatch(@Param("hotSearchIds") int[] hotSearchIds);

//10.查询热搜
    List<HotSearchPostDTO> searchHotSearch(@Param("keyword") String keyword,@Param("start") int start,@Param("pageSize") int pageSize);

/*//11.获取所有活跃的热搜
@Select("select hot_search_id,keyword,heat,is_blocked,is_limited,limit_rate as limitRate,limit_end_time as limitEndTime,limit_start_time as limitStartTime " +
        "from hot_search where is_blocked = 0 AND is_limited = 1 AND limit_start_time IS NOT NULL AND limit_end_time IS NOT NULL AND NOW() BETWEEN limit_start_time AND limit_end_time;")
    List<HotSearchPostDTO> getAllActiveHotSearch();*/

/* 管理员对标签的管理 */

//1.获取标签表
@Select("select tag_id,tag_name,tag_description,tag_status,post_number from tag;")
   List<Tag> getAllTag();

//获取每个标签下文章的数量
@Update("update tag t set t.post_number = (select count(*) from post p where p.tag_id = t.tag_id );")
    void updatePostNumber();

//2.添加标签
@Insert("insert into tag (tag_name,tag_description) values (#{tagName},#{tagDescription});")
    void insertTag(@Param("tagName") String tagName,@Param("tagDescription") String tagDescription);

//3.修改标签状态
@Update("update tag set tag_status=#{tagStatus} where tag_id = #{tagId};")
    void updateTag(@Param("tagId") int tagId,@Param("tagStatus") int tagStatus);

//4.删除标签
@Delete("delete from tag where tag_id=#{tagId};")
     void deleteTagById(@Param("tagId") int tagId);

//5.批量删除标签
  void deleteTagBatch(@Param("tagIds") int []tagIds);

//模糊查询标签
    List<Tag> searchTag(@Param("keyword") String keyword);

//6.根据标签id获取标签信息
@Select("select tag_id,tag_name,tag_description,tag_status,post_number from tag where tag_id=#{tagId};")
    Tag getTagByTagId(@Param("tagId") int tagId);

//7.根据post的Id获取标签Id
@Select("select tag_id from post where post_id=#{postId}")
int getTagIdByPostId(@Param("postId") int postId);

//编辑标签
    void editTagByTagId(@Param("tag") Tag tag);


/* 管理员审核 */

//1.获取待审核的新闻列表(分页查询)
@Select("select p.post_id as postId,p.title as postTitle,u.username as postAuthor,p.content as postContent,t.tag_name as postTag,p.cover as postCover,p.created_time as createdTime," +
        "p.is_member_only as isMemberOnly from post p join user u on p.user_id=u.user_id join tag t on " +
        "p.tag_id=t.tag_id where p.audit_status = 0 order by p.created_time desc limit #{start},#{pageSize};")
   List<AuditPostDTO> getAuditPostList(@Param("start") int start, @Param("pageSize") int pageSize);

//2.获取待审核的新闻总数(用于分页)
@Select("select count(*) from post where audit_status = 0;")
     int getAuditPostCount();

//根据关键词获取待审核的新闻总数
@Select("select count(*) from post where audit_status = 0 and (title like concat('%',#{keyword},'%') or content like concat('%',#{keyword},'%'))")
    int getAuditPostCountByKeyword(@Param("keyword") String keyword);

//查询审核新闻
    List<AuditPostDTO> searchAuditPost(@Param("keyword") String keyword,@Param("start") int start,@Param("pageSize") int pageSize);

//3.审核通过
@Update("update post set audit_status = 1 where post_id=#{postId};")
    void updatePostAudit(@Param("postId") int postId);

//4.审核不通过
@Update("update post set audit_status = 2 where post_id=#{postId};")
    void updatePostNotAudit(@Param("postId") int postId);

//5.批量审核通过
     void updatePostAuditBatch(@Param("postIds") int []postIds);

//6.批量审核不通过
     void updatePostNotAuditBatch(@Param("postIds") int []postIds,@Param("auditReason") String auditReason);


//7.将审核记录上传至审核记录表中
@Insert("insert into audit_post (post_id,admin_name,admin_id,tag_name,tag_id,audit_result,audit_reason,target_type,audit_time) " +
        " values (#{auditPost.postId},#{auditPost.adminName},#{auditPost.adminId},#{auditPost.tagName},#{auditPost.tagId},#{auditPost.auditResult},#{auditPost.auditReason},'news',#{auditPost.auditTime});")
void insertAuditLog(@Param("auditPost") AuditPost auditPost);


//9.获取审核记录分页列表(用于分页)
@Select("select audit_id,post_id,admin_id,admin_name,tag_id,tag_name,target_type,audit_result,audit_reason,audit_time " +
        "from audit_post order by audit_time desc limit #{start},#{pageSize};")
    List<AuditPost> getAuditPosts(@Param("start") int start,@Param("pageSize") int pageSize);

//10.获取审核记录总数(用于分页)
@Select("select count(*) from audit_post;")
    int getAuditAllPosts();

//11.查询审核记录
   List<AuditPost> getAuditPostByKeyword(@Param("keyword") String keyword,@Param("start") int start,@Param("pageSize") int pageSize);

//根据关键词获取审核记录总数
@Select("select count(*) from audit_post where post_id like concat ('%',#{keyword},'%') " +
        "or admin_name like concat ('%',#{keyword}, '%') or tag_name like concat('%',#{keyword}, '%') " +
        "or audit_reason like concat('%',#{keyword}, '%') or target_type like concat('%',#{keyword}, '%');")
    int getAuditPostLogCountByKeyword(@Param("keyword") String keyword);

//12.根据id获取审核记录
@Select("select audit_id,post_id,admin_id,audit_result,audit_reason,audit_time from audit_post where audit_id=#{auditId};")
   AuditPost getAuditPostById(@Param("auditId") int auditId);

//13.批量删除审核记录
   void deleteAuditPostBatch(@Param("auditIds") int []auditIds);

//14.根据id删除审核记录
@Delete("delete from audit_post where audit_id=#{auditId};")
   void deleteAuditPostById(@Param("auditId") int auditId);






 /*
  * 管理员要获取待审核的新闻信息(新闻是那些没有审核的),
  *
  * 1.包括新闻的 标题,内容(若过长,另显示在弹窗中),创建时间,作者(联表user查),类别(联表category查),标签(联表tag查),封面图片,是否为会员专属
  *
  * 2.管理员可以对新闻进行审核(配两个按钮),审核通过后,将新闻的审核状态改为1
  *  审核不通过后,将新闻的审核状态改为2(弹出一个对话框,让管理员填写审核不通过的原因)
  *
  * 3.管理员每审核一次,将该审核记录存入audit_post表中,包括 审核人(管理员id),审核新闻id,审核时间,审核状态(),审核原因
  *
  * 4.前端页面如何显示表示该新闻已经审核过了
  *
  *
  *
  *
  * "insert into audit_post (post_id,admin_id,tag_id,audit_result,audit_reason) " +
        "select p.post_id,u.user_id as adminId,t.tag_id,a.audit_result,a.audit_reason " +
        "from post p join audit_post a on p.post_id=a.post_id join user u on u.user_id=a.admin_id " +
        "join tag t on t.tag_id=a.tag_id where p.post_id = #{auditPost.postId} ;
  */




}